<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<label>开关序号：<input id="num"/></label>（数字）
<br>
<button id="on">on</button>
<button id="off">off</button>
<button id="look">查询</button>
<button id="all">获取cookie完整表示</button>
<button id="list">计算已设置开的状态序号</button>
<h1>cookie串</h1>
<input id="re" style="width:100%"/>
<h1>二进制串</h1>
<div id="binary" style="border:1px solid green;height:20px;"></div>

<h1>开位数列表</h1>
<div id="list_re" style="border:1px solid green;height:20px;"></div>

<script type="text/javascript" src="../../base/javascript/ks-core.js"></script>
<script type="text/javascript">
    var S = KISSY
            ,num = S.one("#num")
            ,on = S.one("#on")
            ,list=S.one("#list")
            ,listRe=S.one("#list_re")
            ,re = S.one("#re")
            ,off = S.one("#off")
            ,binary = S.one("#binary")
            ,trans = S.one("#trans")
            ,all = S.one("#all")
            ,all_one = (Math.pow(2, 32) - 1)
            ,look = S.one("#look");
            
    var store = [0,0,0];
    function check() {
        if (!/\d+/.test(num.val())) {
            alert("开关请输入整数！");
            return;
        }
        var n = parseInt(num.val());
        if (n > store.length * 32) {
            alert("最多：" + store.length * 32);
            return;
        }
        return n;

    }
    look.on("click", function() {
        var num = check();
        if (typeof num !== "number") return;
        var index = Math.floor(num / 32),
                index2 = num % 32,
                mask = Math.pow(2, index2);
        alert(store[index] & mask ? "on" : "off");
    });
    on.on("click", function() {
        var num = check();
        if (typeof num !== "number") return;
        var index = Math.floor(num / 32),
                index2 = num % 32,
                mask = Math.pow(2, index2);
        store[index] |= mask;
        alert("设置成功");
    });
    off.on("click", function() {
        var num = check();
        if (typeof num !== "number") return;
        var index = Math.floor(num / 32),
                index2 = num % 32,
                mask = all_one ^ Math.pow(2, index2);
        store[index] &= mask;
        alert("设置成功");
    });

    function getByte(i, index) {
        return String.fromCharCode((i >> (index * 8)) & 0xFF);
    }
    function padding(i, n) {
        n = n || 32;
        while (i.length < n) {
            i = "0" + i;
        }
        return i;
    }
    all.on("click", function() {
        var str = [],b = [];
        for (var i = 0; i < store.length; i++) {
            var n = store[i];
            for (var j = 3; j >= 0; j--) {
                str.push(getByte(n, j));
            }
        }
        re.val(str.join(""));
        S.Cookie.set("test", str.join(""));
        var v = S.Cookie.get("test");
        for (var i = 0; i < v.length; i++) {
            var n = v[i].charCodeAt(0);
            b.push(padding(n.toString(2), 8));
        }
        binary.html(b.join(""))
    });
    
    var bton={};
    var base=1;
    for(var i=0;i<32;i++){
    	bton[base]=i;
    	base=base<<1;
    }
    
    //fastest one :
    function getOnes(n,base,re){
    	base=base||0;
    	while(n){
    		var mask=n&-n;
    		re.push(bton[mask]+base);
    		n &= ~mask;
    	}
    }
    
    //slower    
    function getOnesSlow(n,base,re) {    	
    	base=base||0;
    	var mask=1;
    	for(var i=0;i<32;i++){
    		if(n&mask) re.push(i);
    		mask=mask<<1;	
    	}
    }
    
    list.on('click',function(){
    	 var re=[];
    	 for (var i = 0; i < store.length; i++) {
            var n = store[i];
       			getOnes(n,i*32,re);
       }
       listRe.html(re.join(","));
    });


</script>
</body>
</html>